package swnxb.小组合作;

/**
 * Created by V5470-I5 on 2016/6/19.
 */
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import swnxb.小组合作.初设计.Vote;
import swnxb.小组合作.初设计.VoteOption;
import swnxb.小组合作.初设计.VoteResult;

public class ShowVoteAction extends ActionSupport {
    private int currentPage;

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public String execute() throws Exception {
        VoteDAO voteDAO = VoteDAOFactory.getVoteDAOInstance();
        VoteOptionDAO voteOptionDAO = VoteOptionDAOFactory
                .getVoteOptionDAOInstance();
        int totalCount = voteDAO.findAllCount();
        Page page = PageUtil.createPage(10, totalCount, currentPage);
        List<Vote> votes = voteDAO.findAllVote(page);
        List<VoteResult> voteResultList = new ArrayList<VoteResult>();
        for (Vote vote : votes) {
            List<VoteOption> voteOptions = voteOptionDAO
                    .findVoteOptionByVoteID(vote.getVoteID());
            VoteResult voteResult = new VoteResult();
            voteResult.setVote(vote);
            voteResult.setVoteOptions(voteOptions);
            voteResultList.add(voteResult);
        }
        HttpServletRequest request = ServletActionContext.getRequest();
        request.setAttribute("voteResultList",voteResultList);
        request.setAttribute("page",page);
        return this.SUCCESS;
    }
}
